<?php
	$_uname = $_GET['username'];
	$_email = $_POST['email'];
	if (isset($_POST['avatar'])) $_ava = $_FILES["avatar"]["name"];
		else $_ava=$_FILES["avatar"]["name"];
	$_about = $_POST['about'];
	$erremail="";
	$errava="";
	$eunik=true;
	require("dbfunc.php");
	
	$qres = mysql_query("SELECT * FROM user");
	while($row = mysql_fetch_array($qres))
	{
		if (strcmp($row['email'],$_email)==0) $eunik=false;
	}
	
	if (strlen($_email)==0){
		$erremail="Email harus diisi";
	}else if (!preg_match('/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,})$/', $_email)){
		$erremail="Email tidak sesuai dengan ketentuan";
	}else if (!$eunik) {
		$erremail="Email sudah terdaftar";
	}
	if (strlen($_ava)==0){
		$errava="Belum memilih gambar";
	}else{
		if (($_FILES["avatar"]["type"] == "image/jpeg")||($_FILES["avatar"]["type"] == "image/pjpeg"))
			$errava="";
		else
			$errava="Jenis file tidak sesuai";
	}
	// echo "error email : ".$erremail;
	// echo "</br>error email : ".$errava;
	// echo "</br>error email : ".$eunik;
		//masukin database
		if($errava=="") {
			echo "Jenis File Sesuai";
			move_uploaded_file($_FILES['avatar']['tmp_name'],"ava/".$_uname.".jpg");
			$qres1="UPDATE user SET avatar='{$_ava}'";
		}
		$qres2="UPDATE user SET email='{$_email}', about='{$_about}'";
		if (!mysql_query($qres2,$con))
		{
			die('Error: ' . mysql_error());
		}
		
		sleep(1);
		header("Location: profile.php?uid=$_uname");
	mysql_close($con);
?>